@extends('layouts.default')

@section('title', '班队会')

@push('scripts')
<script>
    var filters = {};
    var per_page = 10;
    var totalNum = 0;
    layui.use(['layer','form','table','laytpl','laydate','element','laypage'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var element = layui.element;
        var laypage = layui.laypage;
        
        renderSelect();

        // 搜索查询
        form.on('submit(queryBtn)', function(data) {
            var param = data.field;
            filters = param
            getList(1, per_page);
        });
        // 重置
        form.on('submit(resetBtn)', function(data) {
            filters = {};
            $('#qryForm')[0].reset();
            getList(1, per_page)
        });
        form.on('submit(exportBtn)', function(data) {
            var param = data.field;
            var para = {
                "search": param
            };
            
            console.log(filters, '导出');
            // exportDiscipline(filters, function(res) {
            //     if (res.code == '9') {
            //         layer.msg(res.msg, {icon: 5, shift: 6});
            //     } else {
            //         exportsDel('任教学科信息列表.xlsx', '/download/excel/'+res.data.file_name);
            //     }
            // });
        });
        // 获取页面数据
        function getList(page, limit) {
            if ($.isEmptyObject(filters)){
                let param = form.val("qryForm");
                filters = param;
            }
            
            var para = {
                "page": page,
                "search": filters,
                "limit": limit,
                "form_key": "EW_CLASS_MEETING_TABLE"
            };

            getActivityList(para, function(res){
                if(res.code === 1) {
                    if(totalNum != res.total) {
                        totalNum = res.total;
                        per_page = res.limit;
                        renderPageBar(page,per_page)
                    }

                    table.render({
                        data: res.data,
                        elem: '#tablelsit',
                        toolbar: '#toolbarHead', //开启头部工具栏，并为其绑定左侧模板
                        defaultToolbar: [],
                        cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                        limit: res.limit,
                        skin: 'nob',
                        cols: [[
                            {field: 'term_name', title: '学年学期', align: 'left', templet: function(d) {
                                let termName = d.data_version_id ? d.term.title : '-';
                                return d.data_version_name ? d.data_version_name : termName;
                            }},
                            {field: 'name', title: '名称', align: 'center'},
                            {field: 'date', title: '日期', align: 'center'},
                            {field: 'grade_name', title: '年级', align: 'center', templet: function(d) {
                                return d.grade_array.length > 0 ? d.grade_array[0].selectValue : '-';
                            }},
                            {field: 'class_name', title: '班级', align: 'center', templet: function(d) {
                                return d.class_item_set.length > 0 ? d.class_item_set[0].selectValue : '-';
                            }},
                            {field: 'reception_class_name', title: '接待课', align: 'center', templet: function(d) {
                                return d.reception_class_set.length > 0 ? d.reception_class_set[0].selectValue : '-';
                            }},
                            {field: 'status_name', title: '状态', align: 'center', templet: function(d) {
                                switch(d.status) {
                                    case 'REVOKE':
                                        return '撤回';
                                        break;
                                    case 'FILL_IN':
                                        return '未提交';
                                        break;
                                    case 'REVIEW':
                                        return '待审核';
                                        break;
                                    case 'REVIEW_FAIL':
                                        return '已驳回';
                                        break;
                                    case 'PASS':
                                        return '审核通过';
                                        break;
                                    default:
                                        return '-';
                                        break;
                                }
                            }},
                            {title: '操作', width: '20%', align: 'center', toolbar: '#opDemo', fixed:'right'}
                        ]],
                        id:"tableIns",
                        done: function(res, curr, count) {
                            setBtnPermission();
                            curr_page = page;
                        },
                        page: false,
                        loading: true
                    });
                } else {
                    layer.msg(res.msg, {time: 2000, icon: 5});
                }
            });
        }

        // 渲染页面
        function renderPageBar(page,limit) {
            laypage.render({
                elem: 'pageBar',
                count: totalNum, //数据总数，从服务端得到
                curr: page,
                first: '第一页',
                last: '最后一页',
                limit:limit,
                layout: ['limit','count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj, first) {
                    //首次不执行
                    if(!first) {
                        layer.closeAll();
                        getList(obj.curr,obj.limit)
                    }
                }
            });
        }

        //表格头工具栏事件
        table.on('toolbar(tablelsit)', function(obj){
            switch(obj.event){
                case 'export':
                    console.log('导出1')      
                    // exportDiscipline({}, function(res) {
                    //     if (res.code == '9') {
                    //         layer.msg(res.msg, {icon: 5, shift: 6});
                    //     } else {
                    //         exportsDel('任教学科信息列表.xlsx', '/download/excel/'+res.data.file_name);
                    //     }
                    // });
                    break;
            };
        });
        //监听行工具事件
        table.on('tool(tablelsit)', function(obj){
            var data = obj.data;

            if(obj.event === 'del'){
                layer.confirm('确定删除吗?', function(index){
                    delActivity({id: data.id, activity_category: data.activity_category}, function(res) {
                        if (res.code == '9') {
                            layer.msg(res.msg, {icon: 5, shift: 6});
                        } else {
                            layer.msg('删除成功', {icon: 6,time: 1000},function(){
                                obj.del();
                                layer.close(index);
                            });
                        }
                    });
                });
            }
        });
        
        function exportsDel(showName,fileName){
            window.location.href = "{{ route('download.FileDownloadDel') }}?filename="+showName+"&file_url="+fileName;
        }

        //下拉框初始化
        async function renderSelect(){
            await renderDictionarySelect();
            await renderTermSelect();
            getList(1, per_page);
        }

        //字典下拉框数据渲染
        function renderDictionarySelect() {
            return new Promise((resolve) => {
                getDictionaryItem({'diccode': ['SUBJECT', 'GRADE', 'CLASSITEM']}, function (res){
                    if(res.code === 1) {  
                        let data = res.data;

                        for(var k in data) {
                            let key = k.toLowerCase();
                            let d = data[k];
                            
                            $('[data-role='+key+'List]').empty().append('<option value="">请选择</option>');
                            for (var i in d) {
                                $('[data-role='+key+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                            }
                        }
                        
                        form.render(); 
                        
                        resolve();
                    }
                });
            });
        }
        //学期下拉框数据渲染
        function renderTermSelect() {
            return new Promise((resolve) => {
                getTermData({},function(res){
                    if(res.code == 1){
                        $('[data-role=termList]').empty().append('<option value="">请选择</option>');
                        for(var i in res.data){
                            $('[data-role=termList]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                        }
                                      
                        form.render('select');

                        resolve();
                    }
                });  
            });
        }

    });
</script>
@endpush

@section('content')
<style>
    
</style>

<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <form id="qryForm" class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline"> 
                        <label class="layui-form-label">学年学期：</label>
                        <div class="layui-input-inline">
                            <select name="data_version_id" lay-filter="termList" data-role="termList"></select>
                        </div>
                    </div>
                    <div class="layui-inline"> 
                        <label class="layui-form-label">名称：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" placeholder="请输入" class="layui-input" autocomplete="off">    
                        </div>
                    </div>
                    <!-- <div class="layui-inline"> 
                        <label class="layui-form-label">教师姓名：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="teacher_name" placeholder="请输入" class="layui-input" autocomplete="off">    
                        </div>
                    </div> -->
                    <div class="layui-inline"> 
                        <label class="layui-form-label">年级：</label>
                        <div class="layui-input-inline">
                            <select name="grade" lay-filter="gradeList" data-role="gradeList"></select>
                        </div>
                    </div>
                    <div class="layui-inline"> 
                        <label class="layui-form-label">班级：</label>
                        <div class="layui-input-inline">
                            <select name="class_item" lay-filter="classitemList" data-role="classitemList"></select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" >状态:</label>
                        <div class="layui-input-inline">                  
                            <select name="status" lay-filter="" data-role="">
                                <option value="">请选择</option>
                                <option value="FILL_IN">未提交</option>
                                <option value="REVIEW">待审核</option>
                                <option value="REVIEW_FAIL">已驳回</option>
                                <option value="PASS">审核通过</option>
                            </select>                 
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="button" lay-submit lay-filter="queryBtn" class="layui-btn layui-btn-normal">查询</button>
                        <button type="button" lay-submit lay-filter="resetBtn" class="layui-btn layui-btn-primary">重置</button>
                        <button btnPermission="" type="button" lay-submit lay-filter="exportBtn" class="layui-btn layui-btn-normal">导出</button>
                    </div>
                </div>
            </form>

        </div>
    </div>
</div>

<div class="layui-card">
    <script type="text/html" id="toolbarHead">
        <div class="list-head">
            <h3>班队会列表</h3>
            <div class="layui-btn-container list-opt-btns">
                <a btnPermission="273" class="layui-btn layui-btn-normal" href="{{route('meeting.addView')}}" id="addBtn"> + 新增 </a>
                <!-- <a class="layui-btn layui-btn-normal" href="javascript:void(0);" id="importBtn" lay-event="export"> 汇总导出 </a> -->
            </div>
        </div>
    </script>
 
    <div class="layui-card-body">
        <table class="layui-hide" id="tablelsit" lay-filter="tablelsit" lay-data="{id: 'tablelsit'}"></table>
        <div class="pagebarbox" style="text-align: end"><div id="pageBar" class="pagebar"></div></div>
    </div>

    <script type="text/html" id="opDemo">
        <a btnPermission="272" class="layui-btn layui-btn-primary layui-border-orange layui-btn-xs" href="{{route('meeting.infoView')}}?id=@{{d.id}}">查看</a>
        @{{# if(d.instance.status == 'REVIEW'){ }}
        <a btnPermission="275" class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" href="{{route('meeting.checkView')}}?id=@{{d.id}}">审核</a>
        @{{# }else{ }}
        <a btnPermission="275" class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs layui-btn-disabled">审核</a>
        @{{# } }}

        @{{# if(d.instance.status == 'PASS'){ }}
            <a btnPermission="274" class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs layui-btn-disabled">编辑</a>
            <a btnPermission="276" class="layui-btn layui-btn-primary layui-btn-xs layui-btn-disabled">删除</a>
        @{{# }else{ }}
            <a btnPermission="274" class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" href="{{route('meeting.editView')}}?id=@{{d.id}}">编辑</a>
            <a btnPermission="276" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del">删除</a>
        @{{# } }}
    </script>
</div>
  
@endsection
